package com.ten.teacher.contorller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ten.common.core.entity.EduTeacher;
import com.ten.common.core.entity.OssEntity;
import com.ten.common.core.entity.TbCheckTeacher;
import com.ten.common.core.vo.Result;
import com.ten.teacher.service.EduTeacherService;
import com.ten.teacher.vo.BeTeacherFromTbMemberVo;
import com.ten.teacher.vo.TeacherListVo;
import com.ten.teacher.vo.TeacherStatusVo;
import com.ten.teacher.vo.TeacherUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author ：Wqw
 * @date ：Created in 2022/12/7 15:50
 * @description：查询讲师列表的controller层
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/core/teacher")
@Api(tags = "讲师列表API接口类")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
    @Autowired
    private OssEntity ossEntity;

    /**
     * 查询讲师列表,模糊查询,分页
     * page 页码
     * limit 每页显示行数
     * TeacherListVo 条件类 用来封装模糊查询和分页*/
    @ApiOperation(value ="查询讲师列表,模糊查询,分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页码数"),
            @ApiImplicitParam(name = "limit",value = "当前页显示行数"),
    })
    @PostMapping("/findTeacher/{page}/{limit}")
    public Result<IPage<EduTeacher>> findTeacher(@PathVariable(required = true) Integer page, @PathVariable(required = true) Integer limit,
                                                 @RequestBody TeacherListVo teacherListVo){
        return eduTeacherService.selectTeacherList(page, limit, teacherListVo);
    }


    /**修改讲师信息
     *TeacherUpdateVo 条件类 */
    @PostMapping("/updateTeacher")
    @ApiOperation(value = "修改讲师信息")
    public Result<EduTeacher> updateTeacher(@RequestBody TeacherUpdateVo teacherUpdateVo){
        return eduTeacherService.updateTeacherById(teacherUpdateVo);
    }


    /**更新讲师状态
     *未通过审核的讲师不能改变状态
     *1，根据讲师id连表查询讲师审核状态
     * 2，根据审核状态判断是否能够修改讲师状态
     *TeacherStatusVo 条件类 */
    @PostMapping("/changeStatus")
    @ApiOperation(value = "更新讲师状态")
    public Result<EduTeacher> changeStatus(@RequestBody TeacherStatusVo teacherStatusVo){
        return eduTeacherService.updateStatus(teacherStatusVo);
    }


    /**
     * 会员申请讲师
     * 需要远程调用讲师微服务中的方法
     * 将编译的属性放入讲师列表不显示，
     * 审核列表中状态为待审核*/
    @ApiOperation(value = "会员申请讲师")
    @PostMapping("/qianaddTeacher")
    public Result<EduTeacher> qianaddTeacher(@RequestBody BeTeacherFromTbMemberVo beTeacherFromTbMemberVo){
        return eduTeacherService.addTeacher(beTeacherFromTbMemberVo);
    }


/*************************************讲师中心************************************************/
    /**根据讲师手机查询讲师信息*/
    @ApiOperation(value = "根据讲师手机号查询讲师信息")
    @ApiImplicitParam(name="mobile",value = "讲师手机号")
    @GetMapping("/findTeacherBYMobile/{mobile}")
    public Result<EduTeacher> findTeacherBYMobile(@PathVariable String mobile){
        return eduTeacherService.getTeacherByMobile(mobile);
    }

    /**根据手机号修改讲师信息*/
    @ApiOperation(value = "根据讲师手机号修改讲师信息")
    @PostMapping("/updateTeacherBYMobile")
    public Result<EduTeacher> updateTeacherBYMobile(@RequestBody TeacherUpdateVo teacherUpdateVo){
        return eduTeacherService.updateTeacherByMember(teacherUpdateVo);
    }

    @PostMapping("uploadMsg")
    @ApiOperation(value = "阿里云上传图片请求网址")
    public String uploadMsg(MultipartFile file) {
        String filename = file.getOriginalFilename();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossEntity.getEndpoint(), ossEntity.getAccessKeyId(), ossEntity.getAccessKeySecret());
        try {
            try {
                String newFileName = UUID.randomUUID() + filename;
                ossClient.putObject(ossEntity.getBucketName(), newFileName, new ByteArrayInputStream(file.getBytes()));
                return "https://" + ossEntity.getBucketName() + "." + ossEntity.getEndpoint() + "/" + newFileName;
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }


/*************************************可供远端调用的微服务************************************************/

    /**根据讲师手机查询讲师信息*/
    @ApiOperation(value = "根据讲师手机号查询讲师信息")
    @ApiImplicitParam(name="mobile",value = "讲师手机号")
    @PostMapping("/getTeacherByMobile/{mobile}")
    public Result<EduTeacher> getTeacherByMobile(@PathVariable String mobile){
        return eduTeacherService.getTeacherByMobile(mobile);
    }
    /**
     * 根据讲师id查询讲师信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据讲师id查询讲师信息")
    @ApiImplicitParam(name="id",value = "讲师id")
    @GetMapping("/getTeacherById/{id}")
    Result<EduTeacher> getTeacherById(@PathVariable String id){
        return eduTeacherService.getTeacherById(id);
    }


}
